package fun.shuofeng.juc.sync;

import java.util.concurrent.TimeUnit;

/**
 * @author 朔风
 * @date 2024-02-01 15:45
 */
public class DeadLock {
    public static void main(String[] args) {

        Object a = new Object();
        Object b = new Object();

        new Thread(() -> {
            synchronized (a) {
                System.out.println(Thread.currentThread().getName() + "持有锁A，尝试获取锁B");
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                synchronized (b) {
                    System.out.println(Thread.currentThread().getName() + " 获取锁B");
                }
            }
        }, "A线程").start();

        new Thread(() -> {
            synchronized (b) {
                System.out.println(Thread.currentThread().getName() + "持有锁B，尝试获取锁A");
                synchronized (a) {
                    System.out.println(Thread.currentThread().getName() + " 获取锁A");
                }
            }
        }, "B线程").start();


    }
}
